package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.Color;
import com.ruoyi.common.core.domain.DeptQuality;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DeptQualityMapper;
import com.ruoyi.system.service.IDeptQualityService;

/**
 * 部门性质表Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-09
 */
@Service
public class DeptQualityServiceImpl implements IDeptQualityService 
{
    @Autowired
    private DeptQualityMapper deptQualityMapper;

    /**
     * 查询部门性质表
     * 
     * @param id 部门性质表主键
     * @return 部门性质表
     */
    @Override
    public DeptQuality selectDeptQualityById(Long id)
    {
        return deptQualityMapper.selectDeptQualityById(id);
    }

    /**
     * 查询部门性质表列表
     * 
     * @param deptQuality 部门性质表
     * @return 部门性质表
     */
    @Override
    @DataScope(DeptQualityAlias = "dq")
    public List<DeptQuality> selectDeptQualityList(DeptQuality deptQuality)
    {
        return deptQualityMapper.selectDeptQualityList(deptQuality);
    }

    @Override
    public List<TreeSelect> selectDeptQualityTree(DeptQuality deptQuality) {
        List<DeptQuality> deptQualityList = SpringUtils.getAopProxy(this).selectDeptQualityList(deptQuality);
        return buildDeptQualityTreeSelect(deptQualityList);
    }

    @Override
    public List<DeptQuality> buildDeptQualityTree(List<DeptQuality> deptQualityList) {
        List<DeptQuality> returnList = new ArrayList<>();
        List<Long> collect = deptQualityList.stream().map(DeptQuality::getId).collect(Collectors.toList());
        for (DeptQuality deptQuality: deptQualityList){
            //如果是顶节点，遍历该父节点下的所以子节的
            if(!collect.contains(deptQuality.getParentId())){
                recursionFn(deptQualityList,deptQuality);
                returnList.add(deptQuality);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = deptQualityList;
        }
        return returnList;
    }

    @Override
    public List<TreeSelect> buildDeptQualityTreeSelect(List<DeptQuality> deptQualityList) {
        List<DeptQuality> deptQualityTree = buildDeptQualityTree(deptQualityList);
        return deptQualityTree.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    private void recursionFn(List<DeptQuality> deptQualityList , DeptQuality deptQuality){
        // 得到子节点列表
        List<DeptQuality> childList = getChildList(deptQualityList,deptQuality);
        deptQuality.setChildrenDeptQuality(childList);
        for (DeptQuality newDeptQuality : childList)
        {
            if (hasChild(childList, newDeptQuality))
            {
                recursionFn(deptQualityList, newDeptQuality);
            }
        }
    }

    private List<DeptQuality> getChildList(List<DeptQuality> deptQualityList, DeptQuality deptQuality){
        List<DeptQuality> returnList = new ArrayList<>();
        Iterator<DeptQuality> iterator = deptQualityList.iterator();
        while (iterator.hasNext()){
            DeptQuality n = (DeptQuality) iterator.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == deptQuality.getId().longValue())
            {
                returnList.add(n);
            }
        }
        return returnList;
    }

    private boolean hasChild(List<DeptQuality> deptQualityList, DeptQuality deptQuality)
    {
        return getChildList(deptQualityList, deptQuality).size() > 0;
    }


}
